package com.pdf.controller;

import com.pdf.dataObject.Pdf;
import com.pdf.exception.PdfException;
import com.pdf.service.PdfService;
import com.pdf.utils.Md5Util;
import com.pdf.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;

/*
 *@ClassName: PdfController
 *@Description: TODO
 *@author: 郑波
 *@date: 2019-04-12 22:28
 *@version: 1.0
 */
@Controller
public class PdfController {
    @Autowired
    private PdfService pdfService;

    @RequestMapping(value = "fileupload", method = RequestMethod.POST)
    public String fileUpload(HttpServletRequest request, HttpServletResponse response,
                            MultipartFile file, Integer startpage) throws IOException {

        String filename = file.getOriginalFilename();
        String lastName = filename.substring(filename.length() - 3);
        if (!lastName.equals("pdf")) {
            throw new PdfException(0, "文件格式不正确");
        }
        System.out.println(lastName);

        String path = "C:/PDF/";
        //path = path.replaceAll("\\\\", "\\/");
        long size = file.getSize();
        //动态设置等待时间
        long time=2000;
        long initTime=20000;
        long realTime = size / initTime;
        time=time+realTime;

        String realName = String.valueOf(size) + file.getOriginalFilename();
        realName=Md5Util.getKey(realName)+".pdf";
        path=path+TimeUtil.getCurTime()+"/";
        File file1=new File(path);
        if (!file1.exists()){
            file1.mkdirs();
        };
        String filepath=path + realName;

        File newFile = new File(filepath);
        file.transferTo(newFile);
        //将path2放入本地cookie
        Cookie pdfName = new Cookie("MdPdfName", realName);
        response.addCookie(pdfName);
        //判定是否是登入用户,并在page为空的时候设置startpage
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("userUid")) {
                    String userUid = cookie.getValue();
                    List<Pdf> pdfList = pdfService.getByUserUid(userUid);
                    if (pdfList  != null || pdfList.size() > 0) {
                        for (Pdf pdf : pdfList) {
                            if (pdf.getFileName().equals(realName)) {
                                if (startpage == null) {
                                    startpage = pdf.getLastPage();
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
        Cookie cookie=new Cookie("userUid","1");
        response.addCookie(cookie);
        String page = "";
        if (startpage != null) {
            page = "&page=" + startpage;
        }
        String url="redirect:http://127.0.0.1:8083/" + "/pdfjs/web/viewer.html?file="+"http://127.0.0.1:8083/"+TimeUtil.getCurTime()+"/"+ realName + page;
        return url;
    }

}
